package com.ksp.wke.kspa04;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

import org.springframework.beans.factory.annotation.Autowired;

import com.ksp.api.SpringUtil;

public class ArtScaThread implements Runnable{

	ConcurrentLinkedQueue<Map<String, Object>> artScaQue;
	
	private static AtomicInteger totalSca = new AtomicInteger(0);
	
	public static ReentrantLock dealLock = new ReentrantLock(true);
	
	public ArtScaThread(){
	}
	
	public ArtScaThread(ConcurrentLinkedQueue<Map<String, Object>> scaque) {
		artScaQue = scaque;
	}
	
	@Override
	public void run() {
		while(!artScaQue.isEmpty()) {
			try {
				dealLock.lock();
				Iterator<Map<String, Object>> iterator = artScaQue.iterator();
				while (iterator.hasNext()) {
					System.out.println("队列中有消息：" + artScaQue.size() + "个");
					dealArtSca(iterator.next());
					artScaQue.poll();
					System.out.println("处理后队列中剩余消息：" + artScaQue.size() + "个");
				} 
			} catch (Exception e) {
				// TODO: handle exception
			} finally {
				dealLock.unlock();
			}
		}
		
	}
	
	
	void dealArtSca(Map<String, Object> wPamMap) {
		Map<String, Object> wUpdMap = new HashMap<>();
		wUpdMap.put("TTL_TCH", wPamMap.get("TTL_TCH").toString());
		
		//System.out.println("队列线程处理开始：");
		KSPA04Mapper kspa04Mapper = SpringUtil.getBean(KSPA04Mapper.class);
		List<Map<String, Object>> qryUsrInfoLst = null ;//kspa04Mapper.qryUsrInfo(wPamMap);
		//System.out.println(qryUsrInfoLst.toString());
		int updArtScnCnt = 0;//kspa04Mapper.updArtScnCnt(wPamMap);
		String ttl = wPamMap.get("TTL_TCH").toString();
		int tt = totalSca.incrementAndGet();
		System.out.println("线程处理：文章浏览量 + 1，总浏览量 ：" + tt);
		
	}

}
